#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>

#define BUFSIZE 8

int main (int argc, char *argv[])
{
    int i, fd, ret;
    fd_set wfds;
    char buf[BUFSIZE];
    struct timeval timeout;

    fd = open("/tmp/bxtmp/w.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd < 0)
    {
        perror("open");
        return 1;
    }

    FD_ZERO(&wfds);

    for (i = 0; i < 10; i++)
    {
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;

        FD_SET(fd, &wfds);
        ret = select(fd+1, NULL, &wfds, NULL, &timeout);
        if (ret < 0)
        {
            perror("select");
            break;
        }
        else if (ret)
        {
            printf("write...\n");
            snprintf(buf, BUFSIZE, "%d\n", i);
            write(fd, buf, strlen(buf));
        }
        else
        {
            printf("Nothing to do.\n");
        }
    }

    close(fd);

    return 0;
}

